package com.gzy.android_douban.ui.me;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gzy.android_douban.R;
import com.gzy.android_douban.databinding.ActivityCollectionsBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionsActivity extends AppCompatActivity {
    private List<TabFragment> fragments;
    private final List<String> tabTitles = Arrays.asList("全部", "爱情", "动画", "动作", "短片", "黑色电影", "奇幻", "纪录片", "家庭片", "惊悚片", "剧情片", "科幻片", "恐怖片", "冒险片", "西部", "悬疑片", "歌舞片", "战争片", "犯罪片", "喜剧片", "成人", "音乐", "历史", "运动", "传记", "真人秀", "脱口秀", "戏曲", "武侠", "古装", "舞台艺术", "儿童", "情色", "微电影", "灾难", "青春", "文艺", "女性", "同性", "励志", "新闻");
    private ActivityCollectionsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCollectionsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initFragments();
        initView();
        init();
    }

    // 初始化控件、ViewPager
    @SuppressLint("ResourceAsColor")
    private void initView() {
        // 设置下划线指示器与文本宽度对齐
        binding.tabLayout.setTabIndicatorFullWidth(true);
        // 设置下划线指示器颜色
        binding.tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));

        // 设置ViewPager的适配器
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(this);
        binding.viewPager.setAdapter(adapter);

        // TabLayout与ViewPager联动
        bindTabLayoutMonitor();
    }

    // 初始化fragments结合
    private void initFragments() {
        fragments = new ArrayList<>();
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("全部--"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("爱情片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("动画片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("动作片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("短片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("黑色电影"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("奇幻片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("纪录片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("家庭片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("惊悚片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("剧情片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("科幻片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("恐怖片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("冒险片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("西部"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("悬疑片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("歌舞片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("战争片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("犯罪片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("喜剧片"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("成人"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("音乐"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("历史"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("运动"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("传记"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("真人秀"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("脱口秀"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("戏曲"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("武侠"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("古装"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("舞台艺术"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("儿童"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("情色"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("微电影"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("灾难"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("青春"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("文艺"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("女性"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("同性"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("励志"));
        fragments.add(com.gzy.android_douban.ui.me.TabFragment.newInstance("新闻"));

    }

    // ViewPage的Adapter类
    public class MyViewPagerAdapter extends FragmentStateAdapter {
        public MyViewPagerAdapter(@NonNull FragmentActivity activity) {
            super(activity);
        }

        // 返回position位置的Fragment
        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }
        // 获取tab的个数
        @Override
        public int getItemCount() {
            return fragments.size();
        }
    }
    // TabLayout与ViewPager联动
    private void bindTabLayoutMonitor() {
        TabLayoutMediator mediator = new TabLayoutMediator(binding.tabLayout, binding.viewPager,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tabCategory, int position) {
                        tabCategory.setText(tabTitles.get(position));
                    }
                });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();
    }

    public void init(){
        binding.imageCollectionsBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CollectionsActivity.this, MeFragment.class);
                finish();
            }
        });
    }
}